//
// HandleResponse.hh for  in /media/CIBOX USB/Zia/api/Http
// 
// Made by johannick renaud
// Login   <renaud_j@epitech.net>
// 
// Started on  Mon Sep  5 01:34:54 2011 johannick renaud
//

#ifndef			CAZIAC_HANDLERESPONSE_HH
# define		CAZIAC_HANDLERESPONSE_HH

/**
 *	\file		HandleResponse.hh
 *	\date		Mon Sep  5 2011
 *	\author		RENAUD J.
 *	\version	1.0
 */

#include		<map>

#include		"LineResponse.hh"
#include		"IWriter.hh"

#define			CAZIAC_ENDL	"\r\n"

namespace		Caziac
{
  namespace		Http
  {
    /**
     *	\class		HandleResponse
     *	\brief		Handle for an HtttpResponse
     */

    class		HandleResponse
    {

    public:

      typedef		std::map<std::string, std::string>	Header;

    private:

      bool			_sendHead;
      LineResponse		_line;
      Header			_header;
      Tools::IWriter *		_writer;

    public:

      /**
       *	\param		writer the Socket writer
       *	\brief		constructor of the HandleResponse
       *
       *	This will set attribue "_writer" for later using
       */

      HandleResponse(Tools::IWriter *writer);

      /**
       *	\brief		destructor of the HandleResponse
       */

      virtual ~HandleResponse();

      /**
       *	\brief		this function get the header of the Response
       *	\return		the header
       *
       *	This function return the header parsed by the user with
       *   the consumeHeaderLine method. This member function is for consulting.
       */

      Header const &		getHeader() const;

      /**
       *	\brief		this function get the LineResponse (method url version)
       *	\return		the line response
       *	\warning	call readResponseHeader before to set the LineResponse attributes
       *
       */

      LineResponse &		getLineResponse();

      /**
       *	\return		the writer
       */

      Tools::IWriter *		getWriter();

      /**
       *	\brief		write The LineRequest, the Header content and an empty line
       *	\warning	this function must be call at the end of Header parsing
       *
       *
       *	for example this code :
       *
       *	\code
       *	
       *	LineResponse	line;
       *	
       *        line.version = "HTTP/1.1";
       *	line.status = Caziac::Http::OK;
       *	Caziac::Http::HttpStatus[LineResponse.status] = "OK";
       *	addDescription("Content-Length", "20")
       *
       *	\endcode
       *
       *	will send this to the reader :
       *
       *	- "HTTP/1.1 200 OK"
       *	- "Content-Length : 20"
       *	- ""
       *
       *	The content is not send to the writer with this function.
       *
       */
      
      void			pack();

      /**
       *	\fn		sendBody(std::string const &bodyline)
       *	\brief		send a body line or an entire body
       *	\warning	if the Header wasn't send it will send automatically the header before
       *	\return		true if the entire line was send
       *	
       *	This function	check the "Content-length" value of the Header, this will
       *	return false if the the length is too big.
       *	For example if you have:
       *
       *	In the header "Content-length : 20"
       *	\code
       *
       *	sendLine("Star wars \r\n");	// First call return true (size = 12)
       *	sendLine("Star wars \r\n");	// Second call return false (size = 12)
       *
       *	\endcode
       *
       *	This will send only the first call
       */

      bool			sendBody(std::string const &bodyPart);

      /**
       *	\fn		addDescription(std::string const &key, std::string const &def)
       *	\brief		add a description to the Header
       */
      
      void			addDescription(std::string const &key, std::string const &def);

      /**
       *	\fn		operator == (bool const)
       *	\brief		[User Implementation]
       *
       *	This function should return true if the LineResponse status is OK
       *	but a USER can redefine this function for some specifications.
       */

      virtual bool		operator == (bool const);


    };
  }
}

#endif
